﻿using Model;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using 纺织贸易管理系统;
namespace Update
{
    class Program
    {
        static void Main()
        {
            Connect.Environmen = "公司";
            Connect.CreatConnectstring();
            var db = Connect.GetSugar();
            Connect.dbcontext = Connect.GetSugar();
            Console.WriteLine("正在检查数据结构");
            Connect.CreatConnect().CheckAllField(new danjumingxitable());//检查单据明细表
            Connect.CreatConnect().CheckAllField(new DanjuTable());//检查单据表
            Connect.CreatConnect().CheckAllField(new StockTable());//检查库存表
            Connect.CreatConnect().CheckAllField(new LwDetail());//检查来往明细
            Connect.CreatConnect().CheckAllField(new RukuTable());//检查垃圾桶表
            Connect.CreatConnect().CheckAllField(new JuanHaoTable());//检查卷号表
            Connect.CreatConnect().CheckAllField(new DeleteLog());//检查删除日志表
            Connect.CreatConnect().CheckAllField(new ManualCode());//检查手工码单表
            Connect.CreatConnect().CheckAllField(new AccessoriesInfo());//检查辅料表
            Connect.CreatConnect().CheckAllField(new Liuzhuancard());//检查流转卡
            Connect.CreatConnect().CheckAllField(new ProofingRequirements());//检查打样单工艺要求
            Connect.CreatConnect().CheckAllField(new RawMaterial());//检查原料
            Connect.CreatConnect().CheckAllField(new ImportAndExportOfRawMaterials());//检查进出明细
            Connect.CreatConnect().CheckAllField(new CustomerComplaints());//检查进出明细
            Connect.CreatConnect().CheckAllField(new CustomerComplaintsDetail());//检查进出明细
            Connect.CreatConnect().CheckAllField(new LXR());//检查联系人
            Connect.CreatConnect().CheckAllField(new TestData());//测试表
            Connect.CreatConnect().CheckAllField(new ScheduleDoc());
            Connect.CreatConnect().CheckAllField(new OrderTable());//订单信息
            Connect.CreatConnect().CheckAllField(new liuzhuanlog());//流转卡日志
            Connect.CreatConnect().CheckAllField(new MachineTable());//流转卡日志
            Connect.CreatConnect().CheckAllField(new ManualCode());//流转卡日志
            //DELETE t1 FROM danjumingxitable t1 JOIN danjumingxitable t2 ON t1.danhao = t2.danhao and t1.ganghao=t2.ganghao and t1.yanse=t2.yanse and t1.danhao like 'L%'  AND t1.id > t2.id
            //Connect.CreatConnect().DoSQL("DELETE t1 FROM liuzhuancard t1 JOIN liuzhuancard t2 ON t1.cardnum = t2.cardnum  AND t1.id > t2.id");
            //Connect.CreatConnect().DoSQL("DELETE t1 FROM danjumingxitable t1 JOIN danjumingxitable t2 ON t1.danhao = t2.danhao and t1.ganghao=t2.ganghao and t1.yanse=t2.yanse and t1.danhao like 'L%'  AND t1.id > t2.id");
          
            Console.WriteLine("检查完毕");
          
            try
            {
                Console.WriteLine("创建原料信息表");

                db.CodeFirst.SetStringDefaultLength(500/*设置varchar默认长度为200*/).InitTables(typeof(RawMaterial));
                Console.WriteLine("创建入库细码表成功");
                Console.WriteLine("创建辅料单据表");
                db.CodeFirst.SetStringDefaultLength(500/*设置varchar默认长度为200*/).InitTables(typeof(AccessoriesInfo));
                Console.WriteLine("创建辅料单据表成功");
                Console.WriteLine("创建原料单据表");
                db.CodeFirst.SetStringDefaultLength(500/*设置varchar默认长度为200*/).InitTables(typeof(ImportAndExportOfRawMaterials));
                Console.WriteLine("创建原料单据表成功");


            }
            catch (Exception ex)
            {
                Console.WriteLine("创建表的时候发生了错误：" + ex.Message);
            }
            if (db.Queryable<MenuTable>().First(x => x.FormName == "原料信息") == null)
            {

                var kcconnect = new SQLHelper.SQLHelper("Server=softkc.cn,1433;uid=sa;pwd=Kc123456;database=FinishDB");
                UpdateHelper.AddMenu(new MenuTable() { FormName = "原料信息", FatherMenu = "基本资料", MenuName = "原料信息" });
                UpdateHelper.AddMenu(new MenuTable() { FormName = "原料库存", FatherMenu = "库存管理", MenuName = "原料库存" });
                UpdateHelper.AddMenu(new MenuTable() { FormName = "原料入库列表", FatherMenu = "采购管理", MenuName = "原料入库单" });
                UpdateHelper.AddColumn("原料信息", string.Empty);
                UpdateHelper.AddColumn("原料库存", string.Empty);
                UpdateHelper.AddColumn("原料选择", string.Empty);
                UpdateHelper.AddColumn("原料入库列表", string.Empty);
                UpdateHelper.AddColumn("销售发货单", "GridView3");
                UpdateHelper.AddColumn("销售发货单", "GridView4");
            }
            try
            {
                if (db.Queryable<MenuTable>().First(x => x.FormName == "原料领用") == null)
                {
                    UpdateHelper.AddMenu(new MenuTable() { FormName = "原料领用列表", FatherMenu = "生产管理", MenuName = "原料领用单" });
                    UpdateHelper.AddColumn("原料领用列表", "gridView1");
                    UpdateHelper.AddColumn("原料领用单", "gridView1");
                    Console.WriteLine("创建原料领用菜单成功");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("创建原料领用单的时候发送了错误:" + ex.Message);
            }
            //try
            //{
            //    //if (db.Queryable<MenuTable>().First(x => x.FormName == "细码库存") == null)
            //    //{
            //        UpdateHelper.AddMenu(new MenuTable() { FormName = "细码库存", FatherMenu = "库存管理", MenuName = "细码库存" });
            //        UpdateHelper.AddColumn("细码库存", "gridView2");
            //        Console.WriteLine("创建细码库存菜单成功");
            //    //}
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine("创建细码的时候发送了错误:" + ex.Message);
            //}
            //try
            //{
            //    //if (db.Queryable<MenuTable>().First(x => x.FormName == "细码库存") == null)
            //    //{
            //    //Connect.CreatConnect().CheckAllField(new JuanHaoTable());//检查卷号表
            //    var juans = db.Queryable<JuanHaoTable>().Where(x => string.IsNullOrWhiteSpace(x.ReceiptNo)).ToList();
            //    if (juans.Count > 0)
            //    {
            //        if (juans.Select(x => x.ReceiptNo).Count() == 0)
            //        {
            //            Connect.CreatConnect().DoSQL("UPDATE JuanHaoTable set state =1 WHERE Ckmc NOT IN (select ID  from StockTable st) and state =0");
            //            Connect.CreatConnect().DoSQL("UPDATE JuanHaoTable set ReceiptNo =(select rkdh from StockTable where id=juanhaotable.Ckmc) WHERE state =0");
            //            Console.WriteLine("细码增加入库单号");
            //        }
            //    }
            //    //}
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine("创建细码的入库单号的时候发送了错误:" + ex.Message);
            //}
            //try
            //{
            //    //if (db.Queryable<MenuTable>().First(x => x.FormName == "细码库存") == null)
            //    //{
            //    UpdateHelper.AddMenu(new MenuTable() { FormName = "原料盘盈列表", FatherMenu = "库存管理", MenuName = "原料盘盈入库" });
            //    UpdateHelper.AddMenu(new MenuTable() { FormName = "原料盘亏列表", FatherMenu = "库存管理", MenuName = "原料盘亏出库" });
            //    UpdateHelper.AddMenu(new MenuTable() { FormName = "原料采购退货列表", FatherMenu = "采购管理", MenuName = "原料采购退货" });
            //    UpdateHelper.AddColumn("原料盘盈入库列表", "gridView1");
            //    UpdateHelper.AddColumn("原料盘亏出库列表", "gridView1");
            //    UpdateHelper.AddColumn("原料采购退货列表", "gridView1");
            //    UpdateHelper.AddColumn("原料盘盈单", "gridView1");
            //    UpdateHelper.AddColumn("原料盘亏单", "gridView1");
            //    UpdateHelper.AddColumn("原料退货单", "gridView1");

            //    //}
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine("数据库更新到1.0.2.13的时候发送了错误:" + ex.Message);
            //}
            //try
            //{
            //    //if (db.Queryable<MenuTable>().First(x => x.FormName == "细码库存") == null)
            //    //{
            //  if( VersionHelper.GetCurrentVersion ()==Version.Parse ("1.0.2.13"))
            //    {
            //        var lxrs=Connect.dbcontext.Queryable<LXR>().Where (x=>x.LX=="供货商"&&x.JE <0).ToList ();
            //        foreach(var lxr in lxrs)
            //        {
            //            lxr.JE =0-lxr.JE;
            //        }
            //        Connect.dbcontext.Updateable(lxrs).ExecuteCommand();
            //    }
            //    //}
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine("数据库更新到1.0.2.13的时候发送了错误:" + ex.Message);
            //}
            //try
            //{
            //    //if (db.Queryable<MenuTable>().First(x => x.FormName == "细码库存") == null)
            //    //{
            //    Connect.dbcontext.Deleteable<MenuTable>().Where(x => x.FormName == "期初建账").ExecuteCommand ();
            //    //UpdateHelper.AddMenu(new MenuTable() { FormName = "期初建账", FatherMenu = "系统信息", MenuName = "期初建账" });
            //    //}
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine("数据库更新到1.0.2.13的时候发送了错误:" + ex.Message);
            //}
            //try
            //{
            //    //if (db.Queryable<MenuTable>().First(x => x.FormName == "细码库存") == null)
            //    //{
            //    if (VersionHelper.GetCurrentVersion() == Version.Parse("0.5.85"))
            //    {
            //        var raws = Connect.dbcontext.Queryable<RawMaterial>().ToList();
            //        foreach(var raw in raws )
            //        {
            //            raw.CanUsedQuantity = raw.InventoryQuantity;
            //        }
            //        Connect.dbcontext.Updateable(raws).ExecuteCommand();
            //        Connect.dbcontext.Deleteable<ColumnTable>().Where (x=>x.GridName == "uiDataGridView1"&&x.FormName== "流程卡查询").ExecuteCommand();
            //        var users = Connect.dbcontext.Queryable<Yhb>().ToList();
            //        var kcconnect = new SQLHelper.SQLHelper("Server=softkc.cn;uid=sa;pwd=Kc123456;database=FinishDB");
            //        var columns = kcconnect.Query<ColumnTable>(x => x.GridName == "uiDataGridView1" && x.FormName == "流程卡查询" && x.UserID == "10001");
            //        foreach (var user in users )
            //        {
            //            foreach (var item in columns)
            //            {
            //                item.UserID = user.YHBH;
            //            }
            //            Connect.dbcontext.Insertable(columns);
            //        }
            //        columns = kcconnect.Query<ColumnTable>(x => x.GridName == "Gridview1" && x.FormName == "打卷报表" && x.UserID == "10001");
            //        foreach (var user in users)
            //        {
            //            foreach (var item in columns)
            //            {
            //                item.UserID = user.YHBH;
            //            }
            //            Connect.dbcontext.Insertable(columns);
            //        }
            //    }
            //    //}
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine("数据库更新到0.5.85的时候发送了错误:" + ex.Message);
            //}
            //try
            //{
            //    //if (db.Queryable<MenuTable>().First(x => x.FormName == "细码库存") == null)
            //    //{
            //    UpdateHelper.AddMenu(new MenuTable() { FormName = "原料销售列表", FatherMenu = "销售管理", MenuName = "原料销售单" });
            //    UpdateHelper.AddMenu(new MenuTable() { FormName = "辅料销售列表", FatherMenu = "销售管理", MenuName = "辅料销售单" });
            //    UpdateHelper.AddMenu(new MenuTable() { FormName = "销售客诉列表", FatherMenu = "销售管理", MenuName = "销售客诉单" });
            //    //}
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine("增加销售客诉单的时候发生了错误:" + ex.Message);
            //}
            //try
            //{
            //    //if (db.Queryable<MenuTable>().First(x => x.FormName == "细码库存") == null)
            //    //{
            //    UpdateHelper.AddMenu(new MenuTable() { FormName = "唛头关联", FatherMenu = "基本资料", MenuName = "唛头管理" });            
            //    //}
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine("增加唛头管理菜单的时候发生了错误:" + ex.Message);
            //}
            try
            {
                if (db.Queryable<MenuTable>().First(x => x.FormName == "销售预出库列表") == null)
                {
                    UpdateHelper.AddMenu(new MenuTable() { FormName = "销售预出库列表", FatherMenu = "销售管理", MenuName = "销售预出库" });
                    UpdateHelper.AddColumn("销售预出库列表", "Gridview1");
                    UpdateHelper.AddColumn("销售预出库", "Gridview1");
                    UpdateHelper.AddColumn("销售预出库", "Gridview2");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("增加销售预出库的时候发生了错误:" + ex.Message);
            }
            try
            {
                if (db.Queryable<MenuTable>().First(x => x.FormName == "配件采购列表") == null)
                {
                    UpdateHelper.AddMenu(new MenuTable() { FormName = "配件采购列表", FatherMenu = "采购管理", MenuName = "配件采购单" });
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("增加配件采购列表的时候发生了错误:" + ex.Message);
            }
            try
            {
                if (db.Queryable<MenuTable>().First(x => x.FormName == "检测报表") == null)
                {
                    UpdateHelper.AddMenu(new MenuTable() { FormName = "检测报表", FatherMenu = "生产管理", MenuName = "检测报表" });
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("增加配件采购列表的时候发生了错误:" + ex.Message);
            }
            try
            {
                if (db.Queryable<MenuTable>().First(x => x.FormName == "原料领用计划列表") == null)
                {
                    UpdateHelper.AddMenu(new MenuTable() { FormName = "原料领用计划列表", FatherMenu = "生产管理", MenuName = "原料领用计划单" });
                    Console.WriteLine("创建原料领用菜单成功");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("增加原料领用计划列表的时候发生了错误:" + ex.Message);
            }
            try
            {

                string currentPath = Directory.GetCurrentDirectory();
                DirectoryInfo directoryInfo = new DirectoryInfo(currentPath);

                // 确保目录存在
                if (directoryInfo.Exists)
                {
                    FileInfo[] files = directoryInfo.GetFiles();
                    foreach (FileInfo file in files)
                    {
                        if (file.Name.Contains("v24.1"))
                        {
                            file.Delete(); // 删除文件
                            Console.WriteLine($"Deleted file: {file.Name}");
                        }
                    }
                }
                else
                {
                    Console.WriteLine($"Directory {currentPath} does not exist.");
                }

            }
            catch
            {
                Console.WriteLine($"删除文件的时候发生了错误");
            }
            //try
            //{

            //    string currentPath = Directory.GetCurrentDirectory() + "\\zh-CN\\";
            //    DirectoryInfo directoryInfo = new DirectoryInfo(currentPath);

            //    // 确保目录存在
            //    if (directoryInfo.Exists)
            //    {
            //        FileInfo[] files = directoryInfo.GetFiles();
            //        foreach (FileInfo file in files)
            //        {
            //            file.Delete(); // 删除文件
            //            Console.WriteLine($"Deleted file: {file.Name}");
            //        }
            //    }
            //    else
            //    {
            //        Console.WriteLine($"Directory {currentPath} does not exist.");
            //    }
            //    Process.Start(Directory.GetCurrentDirectory() + "\\zh-CN.exe");
            //    Process.Start(Directory.GetCurrentDirectory() + "\\zh-Hans.exe");
            //    Thread.Sleep(25000);
            //    File.Delete(Directory.GetCurrentDirectory() + "\\zh-CN.exe");
            //    File.Delete(Directory.GetCurrentDirectory() + "\\zh-Hans.exe");
            //}
            //catch
            //{
            //    Console.WriteLine($"汉化的时候发生了错误");
            //}
            VersionHelper.IncreaseVersion();//增加版本号
            //SettingService.GetSetting(x=>x.settingname== "默认仓库").settingValue;
            Console.WriteLine("更新数据库完成，按任意键退出");
            Console.ReadKey();
        }
    }
}
